package com.learn.mvc.handler;

import com.learn.mvc.entity.Employee;
import com.learn.mvc.entity.Student;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Controller
public class Demo04ParamHandler {
    Logger logger = LoggerFactory.getLogger(this.getClass());

    @RequestMapping("/param/one/name/one/value")
    public String oneNameOneValue(
            // 使用@RequestParam 注解标记 handler 方法的形参
            // SpringMVC 会将获取到的请求参数从形参位置给我们传进来
            @RequestParam(value = "username", required = false, defaultValue = "missing") String username

            // 当请求参数名和形参名一致时，可以省略@RequestParam("username")注解
            // String username
    ) {
        logger.debug("☆" + username);

        return "target";
    }

    @RequestMapping("/param/one/name/multi/value")
    public String oneNameMultiValue(
            @RequestParam("team") List<String> team
    ) {
        team.forEach(System.out::println);
        return "target";
    }

    @RequestMapping("param/form/to/entity")
    public String formToEntity(
            // SpringMVC 会自动调用实体类中的 setXxx() 注入请求参数
            Employee employee
    ) {
        logger.debug(employee.toString());
        return "target";
    }

    @RequestMapping("param/form/to/nested/entity")
    public String formToNestedEntity(
            // SpringMVC懂得自己注入级联属性，只要属性名和对应的getXxx()，setXxx()匹配即可
            Student student
    ) {

        logger.debug(student.toString());
        return "target";
    }

}
